package fr.enseirb.webxml.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Properties;
import java.util.Date;
import java.util.Map;
import java.util.HashMap;

import java.text.DateFormat;

import fr.enseirb.webxml.util.XMLToolkit;
import fr.enseirb.webxml.util.ServletToolkit;

import fr.enseirb.webxml.data.model.Task;
import fr.enseirb.webxml.data.xml.XMLMediator;

/**
 * Servlet implementation class CRUDTaskServlet
 */
public class CRUDTaskServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CRUDTaskServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Properties params = ServletToolkit.parseURLParams(request);

		String uri = request.getRequestURI();
		if (uri.contains("task/create/url")) {
			String taskXML = "<task"
				+ getAttributeOrEmpty(params, "id")
				+ getAttributeOrEmpty(params, "title")
				+ getAttributeOrEmpty(params, "deadline")
				+ getAttributeOrEmpty(params, "priority")
				+ getAttributeOrEmpty(params, "done") + ">";
			taskXML += getChildOrEmpty(params, "description");
			taskXML += getChildOrEmpty(params, "asker");
			taskXML += getChildOrEmpty(params, "owner");
			taskXML += "</task>";

			XMLMediator.addOrModifyTask(taskXML);

			ServletToolkit.writeResponse(response, taskXML);
		}  else if (uri.contains("task/view/xml")) {
			if (!params.containsKey("id")) {
				throw new IllegalArgumentException("Parameter ID manqueant");
			}
			if (!params.containsKey("exportEncoder")) {
				throw new IllegalArgumentException("Parameter exportEncoder manqueant");
			}

			int id = Integer.parseInt(params.getProperty("id"));
			String xml = XMLMediator.getTask(id);

			response.setHeader("Content-Type", "text/plain");
			Map<String, String> xslParams = new HashMap<String, String>();
			xslParams.put("exportEncoder", params.getProperty("exportEncoder"));
			String transformed = XMLToolkit.transformXML(
				xml, "resources/xsl/common/task_export.xsl", xslParams
			);
			ServletToolkit.writeResponse(response, transformed);
		} else {
			String title = "", state = "", xml = "";

			if (uri.contains("task/create")) {
				title = "Nouvelle tache";
				state = "CREATE";
				xml = "<task/>";
			} else if (uri.contains("task/view")) {
				if (!params.containsKey("id")) {
					throw new IllegalArgumentException("Parameter ID manqueant");
				}
				int id = Integer.parseInt(params.getProperty("id"));
				title = "Afficher tache";
				state = "READONLY";
				xml = XMLMediator.getTask(id);
			} else if (uri.contains("task/modify")) {
				if (!params.containsKey("id")) {
					throw new IllegalArgumentException("Parameter ID manqueant");
				}
				int id = Integer.parseInt(params.getProperty("id"));
				title = "Modifier tache";
				state = "MODIFY";
				xml = XMLMediator.getTask(id);
			}

			Map<String, String> xslParams = new HashMap<String, String>();
			xslParams.put("pageState", state);
			xslParams.put("pageTitle", title);
			xslParams.put("htmlTitle", title);
			String transformed = XMLToolkit.transformXML(
				xml, "resources/xsl/common/crud_task.xsl", xslParams
			);
			ServletToolkit.writeResponse(response, transformed);
		}
	}

	protected String getAttributeOrEmpty(Properties params, String k) {
		return params.containsKey(k) ? " " + k + "=\"" + (params.getProperty(k)) + "\"" : "";
	}

	protected String getChildOrEmpty(Properties params, String k) {
		return params.containsKey(k)
			? "<" + k + ">" + params.getProperty(k) + "</" + k + ">"
			: "";
	}
}
